- Published on
polkadotjs-app 导入以太坊格式助记词
- Authors
- Name
- JiGu
- @crypto20x
1. 通过官方nodejs库生成json文件
import fs from "fs";
import { ApiPromise, Keyring, WsProvider, HttpProvider } from "@polkadot/api"
import { mnemonicGenerate, cryptoWaitReady } from "@polkadot/util-crypto";
async function main() {
await cryptoWaitReady();
// 1) 生成助记词
// const mnemonic = mnemonicGenerate(); // 12 words
// const mnemonic = 'knee raccoon silk transfer move tortoise warm right eager travel smooth limb'
const mnemonic = 'immune dirt melody hour say remind era vivid oblige hope issue breeze'
// 2) 使用 ECDSA 类型创建 Keyring
const keyring = new Keyring({ type: "ethereum" });
const pair = keyring.addFromUri(mnemonic, { name: "my-ecdsa-account" }, "ethereum");
// 3) 设置导入时的加密口令
// const password = "strong-passphrase-here";
const password = null;
// 4) 导出 JSON(polkadot.js apps 可直接导入)
// const json = keyring.addPair(pair);
const json1 = keyring.toJson(pair.address)
// console.log('json1:', JSON.stringify(json1, null, 2))
// 5) 写入文件
const filename = `polkadotjs-ecdsa-${pair.address}.json`;
fs.writeFileSync(filename, JSON.stringify(json1, null, 2), "utf-8");
console.log("助记词:", mnemonic);
console.log("地址:", pair.address);
console.log("已写入文件:", filename);
console.log("\n--- JSON 预览 ---\n", JSON.stringify(json1, null, 2).slice(0, 500), "\n...");
}
main().catch(console.error);
2. polkadotjs-app 浏览器生成
在浏览器的 【设置】-》【account options】 -》 【in-browser account creation】 选择 Allow local in-browser account storage。 然后在【账户】界面,就有 【+Account】出现。
Others
https://docs.moonbeam.network/tokens/connect/polkadotjs/ 最好使用私钥,而不是助记忆词,因为polkadot.js用的是bip39,而以太坊用的则是bip32或者bip44。 这会导致生成的地址与metamask生成的地址不一致。